package com.woniuxy.repayment.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.commons.entity.Receivables;
import com.woniuxy.commons.request.ResPage;
import com.woniuxy.commons.service.FeignReceivablesService;
import com.woniuxy.commons.vo.AddAddTimeToReceivablesVo;
import com.woniuxy.commons.vo.ProjectVo;
import com.woniuxy.commons.vo.SearchProjectVo;
import com.woniuxy.repayment.entity.Repayment;
import com.woniuxy.repayment.mapper.RepaymentMapper;
import com.woniuxy.repayment.service.RepaymentService;
import com.woniuxy.repayment.vo.RepaymentVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

@Service
public class RepaymentServiceImpl implements RepaymentService {

    @Resource
    private FeignReceivablesService receivablesService;

    @Resource
    private RepaymentMapper repaymentMapper;

    @Override
    public boolean addRepayment(ProjectVo projectVo) {
        //  查询对应pno的所有t_receivables数据
        List<Receivables> receivablesList = receivablesService.findAllReceivablesByPno(projectVo.getPno()).getData();
        //  获取待还总额、待收本金、待收利息
        BigDecimal paidmoney = new BigDecimal(0.0);
        BigDecimal principalreceivable = new BigDecimal(0.0);
        BigDecimal interestreceivable = new BigDecimal(0.0);
        receivablesList.forEach(receivables -> {
            paidmoney.add(receivables.getTotalmoney());
            principalreceivable.add(receivables.getPrincipalmoney());
            interestreceivable.add(receivables.getInterestmoney());
        });
        Repayment repayment = new Repayment();
        repayment.setFromProjectvo(projectVo);
        repayment.setPaidmoney(paidmoney);
        repayment.setPrincipalreceivable(principalreceivable);
        repayment.setInterestreceivable(interestreceivable);
        //  放入时间
        //  添加t_receivables的预计回款时间
        Date now = new Date();
        getNewDate(now, projectVo.getDeadline() * 30);
        LocalDateTime estimatedRepaymentTime = LocalDateTime.ofInstant(
                getNewDate(now, projectVo.getDeadline() * 30).toInstant(),
                ZoneId.systemDefault()
        );
        repayment.setPredictmoney(estimatedRepaymentTime);
        return repaymentMapper.addRepayment(repayment);
    }

    @Transactional
    @Override
    public boolean checkRepaymentSuccess(String pno) {
        //  查看对应的数据
        Repayment repayment = repaymentMapper.findRepaymentByPno(pno);
        int compare = repayment.getRepaymentmoney().compareTo(repayment.getPaidmoney());
        if (compare >= 0){
            //  修改状态为还款成功
            repaymentMapper.repaymentSuccess(pno);
        }else {
            //  修改状态为逾期
            repaymentMapper.repaymentFalse(pno);
        }
        return true;
    }

    @Override
    public ResPage<List<RepaymentVo>> searchRepayment(SearchProjectVo searchProjectVo) {
        //  分页
        PageHelper.startPage(searchProjectVo.getPage(), searchProjectVo.getSize());
        List<RepaymentVo> list = repaymentMapper.searchRepayment(searchProjectVo);
        // 获取分页信息
        PageInfo<RepaymentVo> pageInfo = new PageInfo<>(list);
        ResPage<List<RepaymentVo>> resPage = new ResPage<>();
        resPage.setCurPage(pageInfo.getPageNum());
        resPage.setSize(pageInfo.getSize());
        resPage.setTotal(pageInfo.getTotal());
        resPage.setTotalPage(pageInfo.getPages());
        resPage.setData(pageInfo.getList());
        return resPage;
    }

    @Override
    public ResPage<List<RepaymentVo>> searchOverdues(SearchProjectVo searchProjectVo) {
        //  分页
        PageHelper.startPage(searchProjectVo.getPage(), searchProjectVo.getSize());
        List<RepaymentVo> list = repaymentMapper.searchOverdues(searchProjectVo);
        // 获取分页信息
        PageInfo<RepaymentVo> pageInfo = new PageInfo<>(list);
        ResPage<List<RepaymentVo>> resPage = new ResPage<>();
        resPage.setCurPage(pageInfo.getPageNum());
        resPage.setSize(pageInfo.getSize());
        resPage.setTotal(pageInfo.getTotal());
        resPage.setTotalPage(pageInfo.getPages());
        resPage.setData(pageInfo.getList());
        return resPage;
    }

    public static Date getNewDate(Date startDate, int i) {
        // TODO Auto-generated method stub
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(startDate);
        calendar.add(Calendar.DATE, i); //把日期往后增加一天,整数  往后推,负数往前移动
        return calendar.getTime();
    }
}
